<?php
namespace Admin\Controller;
use Think\Controller;

class GoodsController extends Controller{
	//轮播图
	public function lunbo(){
		$goods_id = I('get.goods_id');
           
        $Obj = M('security_lunbo'); // 实例化User对象
        //$map = array('status'=>'1');
        $where = "a.goods_id=$goods_id";
        if(I('post.keywords',''))
        {
            $where .= " and supplier_name like '%".I('post.keywords')."%'";
        }
        $count      = $Obj
        ->alias('a')        
        ->order('a.sort desc')
        ->where($where)
        ->count();// 查询满足要求的总记录数
        $Page = getpage($count, 15);
        $list = $Obj
        ->alias('a')
         
        ->order('a.id desc')
        ->where($where)
        ->limit($Page->firstRow, $Page->listRows)
        ->select();
        $this->assign('listinfo',$list);// 赋值数据集
        $this->assign('page',$Page->show());// 赋值分页输出 
        $this->assign('goods_id',$goods_id);
        // $this->assign('supplier_id',$supplier_id);
        $this->display('lunbolist');
	}
	  //轮播添加
    public  function addLunbo()
    {
        $goods_id = I('get.goods_id');
        $supplier_id = I('get.supplier_id'); 
        $this->assign('goods_id',$goods_id);
        $this->assign('supplier_id',$supplier_id);
        $this->display('addlunbo');
    }
    // 轮播添加处理

    public function addLunboSub()
    {
       // var_dump(I('post.face180'));
        if(I('post.face180'))
        {
            $ad_code = I('post.face180');

        }else{
            $ad_code = I('post.img_url');
        }
        $addtime = time();
        $data =array(
            // 'supplier_id'=>$_POST['supplier_id'],
            'goods_id'=>$_POST['goods_id'],
            'lunbo_weizhi'=>$_POST['lunbo_weizhi'],
            'imgurl'=>$ad_code,
            'dzrul'=>$_POST['dzrul'],
            'is_open'=>$_POST['is_open'],
            'sort'=>$_POST['sort'],
            'add_time'=>$addtime,
            );
       // print_r($data);
       $re = M('security_lunbo')->add($data);
        if($re){    
          $this->success('添加成功',U('Goods/lunbo',array('goods_id'=>$_POST['goods_id'],'supplier_id'=>$_POST['supplier_id'])));
        } else {   
         
          $this->error('添加失败');
        }
     }

     //删除轮播
     public function delLunbo()
     {
        $id = I('get.id');
        $re = M('security_lunbo')->where('id='.$id)->delete();
        if($re){    
          $this->success('删除成功');
        } else {   
         
          $this->error('删除失败');
        }
     }
     //修改轮播
     public function editLunbo()
     {
        $id = I('get.id');
        $data = M('security_lunbo')->where('id='.$id)->find();
        $this->assign('data',$data);
        $this->display('editlunbo');
     }
     public function editLunboSub()
     {
        //print_r(I('post.'));
        if(I('post.face180'))
        {
            $ad_code = I('post.face180');

        }else{
            $ad_code = I('post.img_url');
        }
        //imgurl
        $data = I('post.');
        $data['imgurl'] = $ad_code;
        $re = M('security_lunbo')->where('id='.I('post.id'))->save($data);
         if($re!==false){    
           $this->success('修改成功',U('Goods/lunbo',array('goods_id'=>$_POST['goods_id'],'supplier_id'=>$_POST['supplier_id'])));
        } else {   
         
          $this->error('修改失败');
        }
     }
	public function listc(){
		$model=M('Cat_goods');
		$count = $model->where('is_delete=0 and supplier_id=0')->count (); // 总记录数
		// 实例化分页类，传递参数
		$page = new \Think\Page ( $count, 10 ); // 每页显示10个

		// 定义提示文字
		$page->rollPage = 8; // 分页栏每页显示的页数
		$page->lastSuffix = false;
		$page->setConfig ( prev, '上一页' );
		$page->setConfig ( next, '下一页' );
		$page->setConfig ( 'last', '末页' );
		$page->setConfig ( 'first', '首页' );
		
		// 使用show方法生成url
		$show = $page->show ();
		
		// 查询数据
		$data = $model->where('is_delete=0 and supplier_id=0')->order('goods_id desc,sort_order desc')->limit ( $page->firstRow, $page->listRows )->select ();
		// dump($data);exit;
		$this->assign('data',$data);
		$this->assign ( 'show', $show );
		$this->display();
	}
	//获取入驻商商品列表
	public function list_supplier(){
		$model=M('Cat_goods');
		$count = $model->where('is_delete=0 and supplier_id != 0')->count (); // 总记录数
		// 实例化分页类，传递参数
		$page = new \Think\Page ( $count, 10 ); // 每页显示10个

		// 定义提示文字
		$page->rollPage = 8; // 分页栏每页显示的页数
		$page->lastSuffix = false;
		$page->setConfig ( prev, '上一页' );
		$page->setConfig ( next, '下一页' );
		$page->setConfig ( 'last', '末页' );
		$page->setConfig ( 'first', '首页' );
		
		// 使用show方法生成url
		$show = $page->show ();
		
		// 查询数据
		$data = $model->where('is_delete=0 and supplier_id != 0')->order('goods_id desc,sort_order desc')->limit ( $page->firstRow, $page->listRows )->select ();
		foreach($data as $k=>$v){
			//获取商品的店铺信息
			$info=M('Cat_supplier')->find($v['supplier_id']);
			
			$data[$k]['goods_supplier']=$info['supplier_name'];
      $data[$k]['supplier_id']=$info['supplier_id'];
		}
		
		$this->assign('data',$data);
		$this->assign ( 'show', $show );
		$this->display();
	}
	//展示每个商品的详细信息
	public function show_detail(){
		$id=I('get.id');
		$info=M('Cat_goods')->find($id);
		$info['goods_supplier']='平台商品';
		if($info['supplier_id']!=0){
			$s=M('Cat_supplier')->find($info['supplier_id']);
			$info['goods_supplier']=$s['supplier_name'];
		}
		// 查询关联域名
    $fw_url = M('config') -> where(array('name'=>'fw_url')) -> getField('value'); 
    $this->assign('fw_url',$fw_url);
		$this->assign('info',$info);
		$this->display();	
	}
}
?>